/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

LIST_HEAD(type_id_head);
unsigned int type_id_list_size = 0;

struct type_id_st *add_type_id(struct str_id_st *str_idx) {
  struct type_id_st *type_id = malloc(sizeof(struct type_id_st));
  type_id->str_id = str_idx;
  list_add(&type_id->type_id_list, &type_id_head);
  type_id_list_size++;
  return type_id;
}

int otype_id_st_compar(struct otype_id_st *otype_id_1, struct otype_id_st *otype_id_2) {
  if(otype_id_1->type_id->str_id->idx==otype_id_2->type_id->str_id->idx)
    otype_id_1->bool_duplicate=otype_id_2->bool_duplicate=1;
  return otype_id_1->type_id->str_id->idx-otype_id_2->type_id->str_id->idx;
}

void build_otype_id() {
  otype_id_ary = malloc(type_id_list_size*sizeof(struct otype_id_st));
  memset(otype_id_ary, 0, type_id_list_size*sizeof(struct otype_id_st));
  struct list_head *tmp;
  unsigned int i=0;
  list_for_each(tmp, &type_id_head) {
    (otype_id_ary+i)->type_id = (struct type_id_st *)list_entry(tmp, struct type_id_st, type_id_list);
    i++;
  }
  qsort(otype_id_ary, type_id_list_size, sizeof(struct otype_id_st), (int (*)(const void *, const void *))otype_id_st_compar);
  i=1;
  unsigned int major_i = 0, major_idx = 0;
  while(major_i!=type_id_list_size) {
    (otype_id_ary+major_i)->type_id->idx = major_idx;
    uint32_t packed = htole32((otype_id_ary+major_i)->type_id->str_id->idx);
    // FIXME suspect that this BUFFER_WRITE makes the same data occur in the buffer as twice as it should be for repeating the writting on the same buffer with function pack_otype_id altogether. Let only one exist once this duplication is asserted
    BUFFER_WRITE(buffer[NH_TYPE_ID_IDX],buf_len[NH_TYPE_ID_IDX],&packed,buf_offset[NH_TYPE_ID_IDX],sizeof(uint32_t))

    while(i<type_id_list_size && (otype_id_ary+i)->bool_duplicate && (otype_id_ary+major_i)->type_id->str_id->idx==(otype_id_ary+i)->type_id->str_id->idx) {
      (otype_id_ary+i)->duplicate_major = (otype_id_ary+major_i);
      (otype_id_ary+i)->type_id->idx = (otype_id_ary+major_i)->type_id->idx;
      i++;
    }
    (otype_id_ary+major_i)->bool_duplicate=0;   // Holds with the caluse: bool_duplicate can be set after the _compar function that updates this fields was called for the very last time on this position.
    if(i!=type_id_list_size)
      (otype_id_ary+major_i)->next_major = (otype_id_ary+i);
    major_i=i++;
    major_idx++;
  }
  bounds_move(NH_TYPE_ID_IDX, sizeof(uint32_t)/*Storage Designator: ((struct str_id_st)(struct type_id_st).str_id).idx*/*major_idx);
  majors_size_ary[NH_TYPE_ID_IDX] = major_idx;
}

void pack_otype_id() {
  for(struct otype_id_st *otype_id_major = otype_id_ary;otype_id_major!=NULL; otype_id_major=otype_id_major->next_major){
    uint32_t descriptor_idx = htole32(otype_id_major->type_id->str_id->idx);
    BUFFER_WRITE(buffer[NH_TYPE_ID_IDX],buf_len[NH_TYPE_ID_IDX],&descriptor_idx,buf_offset[NH_TYPE_ID_IDX],sizeof(descriptor_idx));
  }
}

lgwseq_t *fullqualif_from_schent(struct search_entry_st *base, lgwseq_t *unqualif) {
// default package, albeit atypical
if(base==NULL) {
    return unqualif;
}
lgwseq_t *fullqualif = malloc...;
switch(base ->search_entry_type) {
    case PACKAGE :
  
fullqualif = lgwseq_concat(3, base->entry_union.pkg->pkg_str_cache, for_lgwseq(1, L"/"), unqualif); 
break;

   case REFTYPE:

fullqualif = lgwseq_concat(3,base->entry_union.reftype->typ_id_target->str_id->str_dt->unpacked_data, for_lgwseq(1, L"$"), unqualif);
break;
}
  return fullqualif;
}

struct search_entry_st *add_package_search_entry(struct token_list_st *token_list) {
	struct search_entry_st *entry = malloc(sizeof(struct search_entry_st)); 
	entry->entry_union.pkg_particles = token_list;
	entry->entry_union.pkg->pkg_str_cache = strpartcatsep(token_list, for_lgwseq(1, L"/"));
	entry->search_entry_type = PACKAGE;
	entry->parent = NULL;
	list_add(&entry->search_entry_list, &package_search_entry_head);
	package_search_entry_list_size++;
}

struct search_entry_st *add_reftype_search_entry(struct type_id_st *type_id_target, lgwseq_t *search_entry_string, struct search_entry_st *parent) {
	struct search_entry_st *entry = malloc(sizeof(struct search_entry_st)); 
	entry->entry_union.reftype.unqualif_Reftyp = search_entry_string;
	entry->entry_union.reftype.type_id_target = type_id_target;
	entry->search_entry_type = REFTYPE;
	entry->parent = parent;
	list_add(&entry->search_entry_list, &reftype_search_entry_head);
	reftype_search_entry_list_size++;
}


